package com.bosssoft.permission.centre.service.impl;


import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.bosssoft.permission.centre.dao.mapper.CompanyMapper;
import com.bosssoft.permission.centre.entity.Company;
import com.bosssoft.permission.centre.pojo.dto.pcp.CompanyDTO;
import com.bosssoft.permission.centre.pojo.query.CompanyQuery;
import com.bosssoft.permission.centre.pojo.query.IdVersionQuery;
import com.bosssoft.permission.centre.pojo.vo.ResourceVO;
import com.bosssoft.permission.centre.pojo.vo.RoleVO;
import com.bosssoft.permission.centre.pojo.vo.UserVO;
import com.bosssoft.permission.centre.service.CompanyService;
import com.bosssoft.permission.centre.service.exception.UserCentreExceptionCode;
import com.sd365.common.core.annotation.stuffer.IdGenerator;
import com.sd365.common.core.common.api.CommonResponse;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.util.BeanException;
import com.sd365.common.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author: LAP
 * @description:
 * @param
 */
@Service
public class CompanyServiceImpl extends AbstractBaseDataServiceImpl implements CompanyService {



    @Autowired
    private static final String TABLE_NAME = "company";


    @Resource
    CompanyMapper companyMapper;

    @Resource
    private IdGenerator idGenerator;

    @Override
    public List<Company> commonQuery(CompanyQuery companyQuery) {
        try {
            List<Company> companies = companyMapper.commonQuery(companyQuery);
            return companies;
        } catch (Exception e) {
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_COMPANY_COMMONQUERY_EXCEPTION_CODE.toString() , e.getMessage());
        }
    }


    @Override
    public Boolean add(@Valid CompanyDTO companyDTO) {
        Company company = BeanUtil.copy(companyDTO, Company.class);
        company.setId(idGenerator.snowflakeId());
        company.setVersion(1L);
        baseDataStuff4Add(company);

        Boolean flag=companyMapper.insert(company)>0;
//        if (flag) {
//            sendMQMessageUtil.SendMessage(ActionType.INSERT, SendMQMessageUtil.EXCHANGE_NAME, TABLE_NAME, company);
//        }
        return flag;
    }

    /**
     * @param id      公司id
     * @param version 版本
     * @return \\
     * @author Yan Huazhi
     * @date 2020/12/17 11:46
     * @version 0.0.1
     */
    @Override
    public Boolean remove(Long id, Long version) {

        try {
            Boolean flag = companyMapper.selectById(id).getId()>0;
            if (flag) {
                Company company = new Company();
                company.setId(id);
                company.setVersion(version);
            }
            return flag;
        } catch (Exception e) {
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_COMPANY_REMOVE_EXCEPTION_CODE.toString(), e.getMessage());
        }
    }

    @Override
    public Boolean removeBatch(List<IdVersionQuery> idVersionQueryList) {
        return null;
    }

    @Override
    public Boolean modify(CompanyDTO companyDTO) {
        Company company = BeanUtil.copy(companyDTO, Company.class);
        baseDataStuff4Updated(company);
        company.setUpdatedTime(new Date());
        Boolean flag = companyMapper.updateCompany(company) > 0;
/*        if (flag) {
            sendMQMessageUtil.SendMessage(ActionType.UPDATE, SendMQMessageUtil.EXCHANGE_NAME, TABLE_NAME, company);
        }*/
        return flag;
    }

    @Override
    public Boolean batchModify(CompanyDTO companyDTO) {
        return null;
    }


    @Override
    public CompanyDTO queryById(Long id) {
        try {
            CompanyDTO companyDTO = null;
            Company company = companyMapper.selectById(id);
            if (company != null) {
                companyDTO = BeanUtil.copy(company, CompanyDTO.class);
            }
            return companyDTO;
        } catch (BeanException ex) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, ex);
        }
    }

    /**
     * 拷贝company记录
     *
     * @param id 公司id
     * @return CompanyDTO
     * @author Yan Huazhi
     * @date 2020/12/17 13:02
     * @version 0.0.1
     */
    @Override
    public CompanyDTO copy(Long id) {
        try {
            Company company = companyMapper.selectById(id);
            if (company != null) {
                CompanyDTO companyDTO = BeanUtil.copy(company, CompanyDTO.class);
                return companyDTO;
            }
        } catch (Exception e) {

        }
        return null;
    }
}
